//
//  XKAddBankCardView.m
//  XKJY
//
//  Created by xiaokang100 on 2017/1/20.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKAddBankCardView.h"

@interface XKAddBankCardView () <UITextFieldDelegate>

// 白背景
@property (nonatomic, strong) UIView *bgViewOne;
@property (nonatomic, strong) UIView *bgViewTwo;
@property (nonatomic, strong) UIView *bgViewThree;

// 提示语
@property (nonatomic, strong) UILabel *remindLabel;

// 提示标题
@property (nonatomic, strong) UILabel *nameTitleLabel;
@property (nonatomic, strong) UILabel *bankCardNumTitleLabel;
@property (nonatomic, strong) UILabel *bankCardNameTitleLabel;

// 输入框
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *bankCardNumTextField;
@property (nonatomic, strong) UITextField *bankCardNameTextField;


//
@property (nonatomic, strong) UIButton *coverBtn;

// 确认按钮
@property (nonatomic, strong) UIButton *confirmBtn;

@end

@implementation XKAddBankCardView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}


#pragma mark - 提交实名信息
- (void)submitInfo:(UIButton *)sender {
    if (self.AddBankCardBlock) {
        self.AddBankCardBlock(self.nameTextField.text, [self.bankCardNumTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""], self.bankCardNameTextField.text);
    }
}

#pragma mark - 选择开户银行
- (void)bankNameSelect:(UIButton *)sender {
    if (self.nameTextField.isFirstResponder) {
        [self.nameTextField resignFirstResponder];
        if (self.BankNameSelectBlock) {
            self.BankNameSelectBlock();
        }
        
    } else if (self.bankCardNumTextField.isFirstResponder) {
        [self.bankCardNumTextField resignFirstResponder];
        if (self.BankNameSelectBlock) {
            self.BankNameSelectBlock();
        }
    } else {
        if (self.BankNameSelectBlock) {
            self.BankNameSelectBlock();
        }
    }
    
    
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField becomeFirstResponder];
}

- (void)textFieldDidChange:(UITextField *)textField {
    
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    if (textField == self.nameTextField) {
        return (text.length > 20) ? NO : YES;
    } else if (textField == self.bankCardNumTextField) {
    
        NSString *text = [self.bankCardNumTextField text];
        
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
        
        text = [text stringByReplacingCharactersInRange:range withString:string];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        NSString *newString = @"";
        while (text.length > 0) {
            
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        
        // 限制长度
        if (newString.length >= 24) {
            return NO;
        }
        
        [self.bankCardNumTextField setText:newString];
        
        return NO;
        
    }
    
    return YES;
}


- (void)createSubviews {
    
    self.bgViewOne = [UIView new];
    self.bgViewOne.backgroundColor = WhiteColor;
    
    self.bgViewTwo = [UIView new];
    self.bgViewTwo.backgroundColor = WhiteColor;
    
    self.bgViewThree = [UIView new];
    self.bgViewThree.backgroundColor = WhiteColor;
    
    
    self.remindLabel = [UILabel getLabWithText:@"小康100提示您:请填写账号本人银行卡信息!"
                                      textFont:Font_13
                                     textColor:RedColor
                                 textAlignment:NSTextAlignmentCenter
                                 numberOfLines:0];
    
    self.nameTitleLabel   = [UILabel getLabWithText:@"开户姓名" textFont:Font_15 textColor:BlackColor];
    self.bankCardNumTitleLabel = [UILabel getLabWithText:@"银行卡号" textFont:Font_15 textColor:BlackColor];
    self.bankCardNameTitleLabel = [UILabel getLabWithText:@"银行名称" textFont:Font_15 textColor:BlackColor];
    
    
    self.nameTextField                  = [[UITextField alloc] init];
    self.nameTextField.font             = Font(15);
    self.nameTextField.keyboardType     = UIKeyboardTypeDefault;
    self.nameTextField.placeholder      = @"请输入开户人姓名";
    self.nameTextField.textColor        = [UIColor blackColor];
    self.nameTextField.delegate         = self;
    self.nameTextField.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.nameTextField.borderStyle      = UITextBorderStyleNone;
    [self.nameTextField addTarget:self
                           action:@selector(textFieldDidChange:)
                 forControlEvents:UIControlEventEditingChanged];
    
    self.bankCardNumTextField                  = [[UITextField alloc] init];
    self.bankCardNumTextField.font             = Font(15);
    self.bankCardNumTextField.keyboardType     = UIKeyboardTypeNumberPad;
    self.bankCardNumTextField.placeholder      = @"请输入银行卡账号";
    self.bankCardNumTextField.textColor        = [UIColor blackColor];
    self.bankCardNumTextField.delegate         = self;
    self.bankCardNumTextField.clearButtonMode  = UITextFieldViewModeWhileEditing;
    self.bankCardNumTextField.borderStyle      = UITextBorderStyleNone;
    [self.bankCardNumTextField addTarget:self
                             action:@selector(textFieldDidChange:)
                   forControlEvents:UIControlEventEditingChanged];
    
    
    self.bankCardNameTextField                  = [[UITextField alloc] init];
    self.bankCardNameTextField.font             = Font(15);
    self.bankCardNameTextField.keyboardType     = UIKeyboardTypeDefault;
    self.bankCardNameTextField.placeholder      = @"点击选择开户银行";
    self.bankCardNameTextField.textColor        = [UIColor blackColor];
    self.bankCardNameTextField.delegate         = self;
    self.bankCardNameTextField.clearButtonMode  = UITextFieldViewModeNever;
    self.bankCardNameTextField.borderStyle      = UITextBorderStyleNone;
    self.bankCardNameTextField.userInteractionEnabled = NO;
    [self.bankCardNameTextField addTarget:self
                                  action:@selector(textFieldDidChange:)
                        forControlEvents:UIControlEventEditingChanged];
    
    
    self.coverBtn = [UIButton new];
    [self.coverBtn addTarget:self action:@selector(bankNameSelect:) forControlEvents:UIControlEventTouchUpInside];
   

    
    self.confirmBtn = [UIButton getClickBtnWithTitle:@"添加"];
    self.confirmBtn.enabled = YES;
    [self.confirmBtn addTarget:self action:@selector(submitInfo:) forControlEvents:UIControlEventTouchUpInside];
    
   
    
    [self addSubview:self.remindLabel];
    
    [self addSubview:self.bgViewOne];
    [self addSubview:self.bgViewTwo];
    [self addSubview:self.bgViewThree];
    
    
    [self addSubview:self.nameTitleLabel];
    [self addSubview:self.bankCardNumTitleLabel];
    [self addSubview:self.bankCardNameTitleLabel];
    
    [self addSubview:self.nameTextField];
    [self addSubview:self.bankCardNumTextField];
    [self addSubview:self.bankCardNameTextField];
    
    [self addSubview:self.coverBtn];
    
    [self addSubview:self.confirmBtn];
    
    
    [self addConstraints];
}

- (void)addConstraints {
    
    
    [self.remindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(20*Scale_H);
        make.left.mas_equalTo(self).offset(30*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
    }];
    
    [self.bgViewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.remindLabel.mas_bottom).offset(20*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.centerY.mas_equalTo(self.bgViewOne);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameTitleLabel.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.nameTitleLabel);
        make.right.mas_equalTo(self).offset(-30*Scale_W);
    }];
    
    [self.bgViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgViewOne.mas_bottom).offset(20*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.bankCardNameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.nameTitleLabel);
        make.centerY.mas_equalTo(self.bgViewTwo);
    }];
    
    [self.bankCardNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bankCardNameTitleLabel.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.bankCardNameTitleLabel);
        make.right.mas_equalTo(self).offset(-30*Scale_W);
    }];
    
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.bankCardNameTextField);
        make.height.mas_equalTo(self.bankCardNameTextField);
        make.width.mas_equalTo(self.bankCardNameTextField).multipliedBy(0.8);
    }];
    
    
    [self.bgViewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgViewTwo.mas_bottom).offset(1*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50*Scale_H);
    }];
    
    [self.bankCardNumTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.nameTitleLabel);
        make.centerY.mas_equalTo(self.bgViewThree);
    }];
    
    [self.bankCardNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bankCardNumTitleLabel.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.bankCardNumTitleLabel);
        make.right.mas_equalTo(self).offset(-30*Scale_W);
    }];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgViewThree.mas_bottom).offset(20*Scale_H);
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
        make.height.mas_equalTo(44*Scale_H);
    }];
    
}

- (void)setBankNameText:(NSString *)bankNameText {
    _bankNameText = bankNameText;
    
    self.bankCardNameTextField.text = bankNameText;
}

@end
